package com.winter.main.service.dept.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winter.main.mapper.master.DeptMapper;
import com.winter.main.model.entity.Dept;
import com.winter.main.service.dept.DeptService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Desc:
 * Author: dushuang
 * Date: Create in 2018/2/28
 */

@Service(value = "deptService")
public class DeptServiceImpl implements DeptService{

    private Logger logger = LogManager.getLogger(DeptServiceImpl.class);

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public PageInfo<Dept> findAllDept(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        logger.info("调用deptMapper.selectAllDept()...");
        List<Dept> result = deptMapper.selectAll();
        PageInfo<Dept> pages = new PageInfo<>(result);
        return pages;
    }

    @Override
    @Cacheable(value = "dept", key = "#root.targetClass.name + '-dept-' +#dept.id", unless = "#result eq null")
    public List<Dept> findByMap(int pageNum, int pageSize, Dept dept) {
        PageHelper.startPage(pageNum,pageSize);
        logger.info("调用deptMapper.selectByMap(dept)...");
        return deptMapper.findDept(dept);
    }



    @Override
    @Transactional
    @CachePut(value = "dept", key = "#root.targetClass.name + '-dept-' + #dept.id")
    public void insertDept(Dept dept) {
        deptMapper.insertSelective(dept);
    }

    @Override
    @Transactional
    @CacheEvict(value = "dept", key = "#root.targetClass.name + '-dept-' + #dept.id", condition = "#result eq true ")
    public boolean deleteDept(Dept dept) {
        return deptMapper.deleteByPrimaryKey(dept) > 0;
    }
}
